﻿using System;
using Bookvar.ObjectModel.Actions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;

namespace Bookvar.UnitTests.ObjectModel.Actions
{
    /// <summary>
    /// Unit tests for the <see cref="CompositeAction"/> class.
    /// </summary>
    [TestClass]
    public class CompositeActionTests
    {
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        #region Additional test attributes

        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //

        #endregion

        [TestMethod]
        public void CanCreateNew()
        {
            var action = new CompositeAction();
            Assert.IsNotNull( action );
        }

        [TestMethod]
        public void EmptyCompositeAction_CanRedo()
        {
            var compositeAction = new CompositeAction();
            Assert.IsTrue( compositeAction.CanRedo );
        }

        [TestMethod]
        public void EmptyCompositeAction_CanUndo()
        {
            var compositeAction = new CompositeAction();
            Assert.IsTrue( compositeAction.CanUndo );
        }

        [TestMethod]
        public void EmptyCompositeAction_Execute_DoesNotThrow()
        {
            var compositeAction = new CompositeAction();
            compositeAction.Execute();
        }

        [TestMethod]
        [ExpectedException( typeof ( ArgumentNullException ) )]
        public void Add_NullAction_Throws()
        {
            var compositeAction = new CompositeAction();
            compositeAction.Add( null );
        }

        [TestMethod]
        public void CanRedo_AllAddedActionsThatCanRedo_ReturnsTrue()
        {
            var action1 = MockRepository.GenerateStub<IAction>();
            action1.Stub( x => x.CanRedo ).Return( true ).Repeat.Any();
            var action2 = MockRepository.GenerateStub<IAction>();
            action2.Stub( x => x.CanRedo ).Return( true ).Repeat.Any();

            var compositeAction = new CompositeAction();
            compositeAction.Add( action1 );
            compositeAction.Add( action2 );

            Assert.IsTrue( compositeAction.CanRedo );
        }

        [TestMethod]
        public void CanUndo_AllAddedActionsThatCanUndo_ReturnsTrue()
        {
            var action1 = MockRepository.GenerateStub<IAction>();
            action1.Stub( x => x.CanUndo ).Return( true ).Repeat.Any();
            var action2 = MockRepository.GenerateStub<IAction>();
            action2.Stub( x => x.CanUndo ).Return( true ).Repeat.Any();

            var compositeAction = new CompositeAction();
            compositeAction.Add( action1 );
            compositeAction.Add( action2 );

            Assert.IsTrue( compositeAction.CanUndo );
        }

        [TestMethod]
        public void CanRedo_OneOfTheAddedActionsCannotRedo_ReturnsFalse()
        {
            var action1 = MockRepository.GenerateStub<IAction>();
            action1.Stub( x => x.CanRedo ).Return( true ).Repeat.Any();
            var action2 = MockRepository.GenerateStub<IAction>();
            action2.Stub( x => x.CanRedo ).Return( false ).Repeat.Any();

            var compositeAction = new CompositeAction();
            compositeAction.Add( action1 );
            compositeAction.Add( action2 );

            Assert.IsFalse( compositeAction.CanRedo );
        }

        [TestMethod]
        public void CanUndo_OneOfTheAddedActionsCannotUndo_ReturnsFalse()
        {
            var action1 = MockRepository.GenerateStub<IAction>();
            action1.Stub( x => x.CanUndo ).Return( true ).Repeat.Any();
            var action2 = MockRepository.GenerateStub<IAction>();
            action2.Stub( x => x.CanUndo ).Return( false ).Repeat.Any();

            var compositeAction = new CompositeAction();
            compositeAction.Add( action1 );
            compositeAction.Add( action2 );

            Assert.IsFalse( compositeAction.CanUndo );
        }

        [TestMethod]
        public void Execute_AfterAnActionIsAdded_ExecutesTheAction()
        {
            var action = MockRepository.GenerateStub<IAction>();

            var compositeAction = new CompositeAction();
            compositeAction.Add( action );
            compositeAction.Execute();

            action.AssertWasCalled( x => x.Execute() );
            action.AssertWasCalled( x => x.Execute() );
        }

        [TestMethod]
        public void Execute_WithTwoAddedActions_ExecutesTheActionsInTheOrderTheyWereAdded()
        {
            var mocks = new MockRepository();
            var firstAction = mocks.StrictMock<IAction>();
            var secondAction = mocks.StrictMock<IAction>();

            using ( mocks.Ordered() )
            {
                Expect.Call( firstAction.Execute );
                Expect.Call( secondAction.Execute );
            }

            mocks.ReplayAll();

            var compositeAction = new CompositeAction();
            compositeAction.Add( firstAction );
            compositeAction.Add( secondAction );
            compositeAction.Execute();

            mocks.VerifyAll();
        }

        [TestMethod]
        public void Undo_AfterExecuteWithTwoAddedActions_UndoTheActionsInTheReverseOrderTheyWereAdded()
        {
            var mocks = new MockRepository();
            var firstAction = mocks.StrictMock<IAction>();
            var secondAction = mocks.StrictMock<IAction>();

            secondAction.Execute();
            LastCall.Repeat.Any();
            firstAction.Execute();
            LastCall.Repeat.Any();

            using ( mocks.Ordered() )
            {
                Expect.Call( secondAction.Undo );
                Expect.Call( firstAction.Undo );
            }

            mocks.ReplayAll();

            var compositeAction = new CompositeAction();
            compositeAction.Add( firstAction );
            compositeAction.Add( secondAction );
            compositeAction.Execute();
            compositeAction.Undo();

            mocks.VerifyAll();
        }

        [TestMethod]
        public void Execute_WithActionAddedAsExecuted_DoesNotExecuteTheAction()
        {
            var action = MockRepository.GenerateStub<IAction>();

            var compositeAction = new CompositeAction();
            compositeAction.AddExecuted( action );
            compositeAction.Execute();

            action.AssertWasNotCalled( x => x.Execute() );
        }

        [TestMethod]
        public void Undo_AfterExecuteOfActionAddedAsExecuted_UndoTheAction()
        {
            var action = MockRepository.GenerateStub<IAction>();

            var compositeAction = new CompositeAction();
            compositeAction.AddExecuted( action );
            compositeAction.Execute();
            compositeAction.Undo();

            action.AssertWasCalled( x => x.Undo() );
            Assert.AreEqual( 1, action.GetArgumentsForCallsMadeOn( x => x.Undo() ).Count );
        }

        [TestMethod]
        public void Execute_AfterUndoOfActionIsAddedAsExecuted_ExecutesTheActionOnlyAfterTheSecondExecute()
        {
            var mocks = new MockRepository();
            var action = mocks.StrictMock<IAction>();

            action.Undo();
            LastCall.Repeat.Once();
            action.Execute();
            LastCall.Repeat.Once(); // execute must be called only once after the compositeAction.Redo().

            mocks.ReplayAll();

            var compositeAction = new CompositeAction();
            compositeAction.AddExecuted( action );
            compositeAction.Execute();
            compositeAction.Undo();
            compositeAction.Execute();

            mocks.VerifyAll();
        }

        [TestMethod]
        public void Execute_AfterUndoOfActionIsAddedAsExecuted_ExecutesTheActionOnlyAfterTheSecondExecute1()
        {
            var action = MockRepository.GenerateStub<IAction>();

            var compositeAction = new CompositeAction();
            compositeAction.AddExecuted( action );
            compositeAction.Execute();
            compositeAction.Undo();
            compositeAction.Execute();

            action.AssertWasCalled( x => x.Undo() );
            Assert.AreEqual( 1, action.GetArgumentsForCallsMadeOn( x => x.Undo() ).Count );

            action.AssertWasCalled( x => x.Execute() );
            Assert.AreEqual( 1, action.GetArgumentsForCallsMadeOn( x => x.Execute() ).Count );
        }
    }
}